const https = require('https')
const http = require('http')
const { createProxyMiddleware } = require("http-proxy-middleware")
const url = require('url')

const server = http.createServer((req, res) => {
    let urlstr = req.url
    if (/\/dp/.test(urlstr)) {
        console.log(urlstr);
        const proxy = createProxyMiddleware('/dp', {
            target: 'https://mst.vip.com',
            changeOrigin: true
        })
        // 假设我们访问的是localhost:8080/dp/assd/aa
        // 用了代理之后会成:https://mst.vip.com/dp/assd/aa  v这种是正向代理
        proxy(req, res)
    } else if (/\/api/.test(urlstr)) {
        console.log(urlstr);
        const proxy2 = createProxyMiddleware('/api', {
            target: 'https://m.lagou.com',
            changeOrigin: true,
            pathRewrite:{
                "^/api":""
            }
            // 假设我们访问的是localhost:8080/api/dp/assd/aa  
            // 用了代理之后会成:https://m.lagou.com/api/dp/assd/aa  但是真正的接口没有 所以需要做路径转发
        })
        proxy2(req, res)
    }

})
server.listen('3000', () => {
    console.log('q');
})
